package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.*;
import com.atguigu.result.Result;
import com.atguigu.service.*;
import com.atguigu.vo.HouseQueryVo;
import com.atguigu.vo.HouseVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-28 8:28
 * @Description:
 */
@RestController
@RequestMapping("/house")
public class HouseController {

    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private HouseImageService houseImageService;
    @Reference
    private UserFollowService userFollowService;

    @RequestMapping("/list/{pageNum}/{pageSize}")
    public Result findListPage(@PathVariable Integer pageNum, //第几页
                               @PathVariable Integer pageSize,//每页记录数
                               @RequestBody HouseQueryVo houseQueryVo){//查询条件
        //this.houseService.findPage()此方法是后台分页查询,查询的结果不一样,所以新方法区分
        PageInfo<HouseVo> pageInfo = houseService.findListPage(pageNum,pageSize,houseQueryVo);

        return Result.ok(pageInfo);
    }

    @RequestMapping("/info/{id}")
    public Result showInfo(@PathVariable Long id, HttpServletRequest request){
        Map map = new HashMap();
        //查询指定id的房源信息
        House house = houseService.getById(id);
        map.put("house",house);
        //查询指定房源的小区信息
        Community community = communityService.getById(house.getCommunityId());
        map.put("community",community);
        //查询指定房源的经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(house.getId());
        map.put("houseBrokerList",houseBrokerList);
        //查询指定房源的图片信息
        List<HouseImage> houseImageList = houseImageService.findList(house.getId(), 1);
        map.put("houseImage1List",houseImageList);

        //判断该房源是否被关注信息

        boolean isFollow = false;
        //判断该用户是否已经关注该房源
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("USER");
        if(userInfo != null){//用户已经登录
            //判断该用户是否已经关注该房源
            int count = userFollowService .isFollowed(userInfo.getId(),id);
            if(count > 0){
                isFollow = true;
            }
        }

        map.put("isFollow",isFollow);
        return Result.ok(map);
    }
}
